package com.ll.menu.user.history.detail;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.droid.library.app.App;
import com.droid.library.app.emptyview.BaseCallback;
import com.droid.library.app.emptyview.BaseEmptyViewManager;
import com.droid.library.app.emptyview.CallbackAdapter;
import com.droid.library.app.mvp.BaseMVPActivity;
import com.droid.library.app.mvp.IUI;
import com.droid.library.app.mvp.list.FooterManager;
import com.droid.library.widget.recyclerview.BaseRecyclerAdapter;
import com.ll.menu.user.R;
import com.ll.menu.user.app.entity.Food;
import com.ll.menu.user.cart.CartManager;
import com.ll.menu.user.dialog.SaleDialog;

import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 用餐记录
 *
 * @author wangheng
 */

public class HistoryDetailActivity extends BaseMVPActivity<HistoryDetailPresenter>
        implements IHistoryDetailUI,View.OnClickListener {

    private static final String KEY_ORDER_ID = "keyOrderId";
    private static final String KEY_ORDER_DATE = "keyOrderDate";

    @InjectView(R.id.flHistoryDetailEmptyContainer)
    ViewGroup mEmptyViewContainer;

    @InjectView(R.id.rvHistoryDetail)
    RecyclerView mRecyclerView;

    @InjectView(R.id.tvHistoryDetailUsername)
    TextView mUsernameTextView;

    @InjectView(R.id.tvHistoryDetailTotalPrice)
    TextView mTotalPriceTextView;

    @InjectView(R.id.tvHistoryDetailDate)
    TextView mDateTextView;

    private HistoryDetailEmptyViewManager mEmptyViewManager;
    private HistoryDetailAdapter mAdapter;

    private String mOrderId = "";

    private String mTotalPriceFormat = null;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_history_detail;
    }

    @Override
    protected void onCreateExecute(Bundle savedInstanceState) {
        mTotalPriceFormat = App.getInstance().getString(R.string.total_price_mode);

        for(int i = 0;i < 20;i++){
            Food food = new Food();
            food.setId(String.valueOf(i * i + 1000));
            food.setName("小炒肉盖饭套餐+蛋炒饭套餐+饮料瓶");
            food.setCount(i % 4 + 1);
            food.setCurrentPrice(33 + i / 10.0f);
            food.setOldPrice(55 + i * i / 10.0f);
            food.setCountDown(i + 100);

            CartManager.getInstance().addFood(food);
        }

        initView();
        getPresenter().requestList();

//        new StatusDialog(this).show();
//        new ManagerDialog(this).show();
//        new LoginDialog(this).show();
//        new ReturnDialog(this).show();
        new SaleDialog(this).show();
    }

    public void initView(){

        Bundle bundle = getIntent().getExtras();
        if(bundle != null){
            String date = bundle.getString(KEY_ORDER_DATE,"");
            mOrderId = bundle.getString(KEY_ORDER_ID,"");

            mDateTextView.setText(date);
        }

        mEmptyViewManager = new HistoryDetailEmptyViewManager(new CallbackAdapter());
        mEmptyViewManager.addEmptyViewToFirst(HistoryDetailActivity.this, mEmptyViewContainer);
        mEmptyViewManager.hideAllEmptyView();

        mRecyclerView.setLayoutManager(
                new GridLayoutManager(HistoryDetailActivity.this, 2));
        mAdapter = new HistoryDetailAdapter(HistoryDetailActivity.this);
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    public BaseRecyclerAdapter<Food> getAdapter() {
        return mAdapter;
    }

    @Override
    public void onPullDownRefreshCompleted() {

    }

    @Override
    public void onPullUpRefreshCompleted() {
    }

    @Override
    public FooterManager getFooterManager() {
        return null;
    }

    @Override
    public Bundle getExtras() {
        return null;
    }

    @Override
    public BaseEmptyViewManager<? extends BaseCallback> getEmptyViewManager() {
        return mEmptyViewManager;
    }

    @Override
    protected IUI getUI() {
        return HistoryDetailActivity.this;
    }

    @Override
    protected HistoryDetailPresenter createPresenter() {
        return new HistoryDetailPresenter();
    }


    @OnClick({R.id.tvHistoryDetailCheck,
            R.id.ivHistoryDetailBack,
            R.id.tvHistoryDetailExitLogin})
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ivHistoryDetailBack:
                finishActivity();
                break;
            case R.id.tvHistoryDetailExitLogin:
                finishActivity();
                break;
            case R.id.tvHistoryDetailCheck:

                break;
            default:
                break;
        }
    }

    @Override
    public void setTotalPrice(float price) {
        if(isFragmentDetached() || mTotalPriceTextView == null){
            return;
        }
        mTotalPriceTextView.setText(String.format(mTotalPriceFormat,price));
    }

    public static void launch(Activity activity,String orderId,String date){
        Intent intent = new Intent(activity,HistoryDetailActivity.class);

        Bundle bundle = new Bundle();
        bundle.putString(KEY_ORDER_ID,orderId);
        bundle.putString(KEY_ORDER_DATE,date);
        intent.putExtras(bundle);

        activity.startActivity(intent);
    }
}
